﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
using System.Drawing;
using System.Net.Mail;

using System.Web.Services;
using AjaxControlToolkit;
using System.IO.MemoryMappedFiles;

public partial class partner_partner_Add_agent : System.Web.UI.Page
{
    string a_id = null;
    private byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, System.Drawing.Imaging.ImageFormat formatOfImage)
    {
        byte[] Ret;
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                imageToConvert.Save(ms, formatOfImage);
                Ret = ms.ToArray();
            }
        }
        catch (Exception) { throw; }
        return Ret;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["email"] == null)
        {
            Response.Redirect("sessiont.aspx");
        }

        Label2.Text = Session["email"].ToString();
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        string cat = null;
        cat = ddlcategory.SelectedValue;
        SqlConnection conn = null;
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
        conn.Open();
        string filename = FileUpload1.PostedFile.FileName.ToString();
        SqlCommand scmd = new SqlCommand("SELECT * FROM agent_add where email='" + txtemail.Text + "'", conn);

        SqlDataReader dr = scmd.ExecuteReader();
        if (dr.Read())
        {

            Label1.Visible = true;


        }


        else
        {
            dr.Close();


            //SqlConnection conn = null;

            //conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);





            Random r1 = new Random();
            int num = r1.Next(0, 99999999); // generate random number
            string number = string.Format("{0:D8}", num);

            Random r2 = new Random();
            int num2 = r2.Next(0, 26); // generate random letter
            char letter = (char)('A' + num2);


            a_id = letter + number; // join both as ID 
            if (filename.Equals(""))
            {
                string ServerMapPath = Server.MapPath("../realia/img/tmp/property-large-1.jpg" + FileUpload1.FileName);
                System.Drawing.Image imag = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);

                SqlCommand cmd = new SqlCommand("Insert into agent_add values(@a_id,@f_name,@m_name,@l_name,@d_o_b,@occu,@h_no,@city,@street,@ssn,@zip_code,@country,@email,@pass,@type,@adding_date,@adding_by,@profile_pic,@status)", conn);
                cmd.Parameters.AddWithValue("@a_id", a_id);
                cmd.Parameters.AddWithValue("@f_name", txtfname1.Text);
                cmd.Parameters.AddWithValue("@m_name", txtmname.Text);
                cmd.Parameters.AddWithValue("@l_name", txtlname1.Text);
                cmd.Parameters.AddWithValue("@d_o_b", txtdob1.Text);
                cmd.Parameters.AddWithValue("@occu", txtoccupation.Text);
                cmd.Parameters.AddWithValue("@h_no", txthno.Text);
                cmd.Parameters.AddWithValue("@city", txtcity.Text);
                cmd.Parameters.AddWithValue("@street", txtstreet.Text);
                cmd.Parameters.AddWithValue("@ssn", txtssn.Text);
                cmd.Parameters.AddWithValue("@zip_code", txtzip.Text);
                cmd.Parameters.AddWithValue("@country", txtcountry.Text);
                cmd.Parameters.AddWithValue("@email", txtemail.Text);
                cmd.Parameters.AddWithValue("@pass", txtpass.Text);
                cmd.Parameters.AddWithValue("@type", ddlcategory.SelectedValue);
                cmd.Parameters.AddWithValue("@adding_date", System.DateTime.Now);
                cmd.Parameters.AddWithValue("@adding_by", Session["email"]);
                cmd.Parameters.Add("@profile_pic", SqlDbType.Image, 0).Value = ConvertImageToByteArray(imag, System.Drawing.Imaging.ImageFormat.Jpeg);
                //cmd.Parameters.Add("@profile_pic", SqlDbType.VarBinary).Value = DBNull.Value;
                cmd.Parameters.AddWithValue("@status", "0");
                cmd.ExecuteNonQuery();
            }
            else
            {
                System.Drawing.Image imag = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
                SqlCommand cmd = new SqlCommand("Insert into agent_add values(@a_id,@f_name,@m_name,@l_name,@d_o_b,@occu,@h_no,@city,@street,@ssn,@zip_code,@country,@email,@pass,@type,@adding_date,@adding_by,@profile_pic,@status)", conn);
                cmd.Parameters.AddWithValue("@a_id", a_id);
                cmd.Parameters.AddWithValue("@f_name", txtfname1.Text);
                cmd.Parameters.AddWithValue("@m_name", txtmname.Text);
                cmd.Parameters.AddWithValue("@l_name", txtlname1.Text);
                cmd.Parameters.AddWithValue("@d_o_b", txtdob1.Text);
                cmd.Parameters.AddWithValue("@occu", txtoccupation.Text);
                cmd.Parameters.AddWithValue("@h_no", txthno.Text);
                cmd.Parameters.AddWithValue("@city", txtcity.Text);
                cmd.Parameters.AddWithValue("@street", txtstreet.Text);
                cmd.Parameters.AddWithValue("@ssn", txtssn.Text);
                cmd.Parameters.AddWithValue("@zip_code", txtzip.Text);
                cmd.Parameters.AddWithValue("@country", txtcountry.Text);
                cmd.Parameters.AddWithValue("@email", txtemail.Text);
                cmd.Parameters.AddWithValue("@pass", txtpass.Text);
                cmd.Parameters.AddWithValue("@type", ddlcategory.SelectedValue);
                cmd.Parameters.AddWithValue("@adding_date", System.DateTime.Now);
                cmd.Parameters.AddWithValue("@adding_by", Session["email"]);
                cmd.Parameters.Add("@profile_pic", SqlDbType.Image, 0).Value = ConvertImageToByteArray(imag, System.Drawing.Imaging.ImageFormat.Jpeg);
                cmd.Parameters.AddWithValue("@status", "0");
                cmd.ExecuteNonQuery();
            }
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("realstate777@gmail.com");
                mail.To.Add(txtemail.Text);
                mail.Subject = "Real state agent account information ";
                mail.Body = "your email-id is:-" + txtemail.Text + "    your password is :-" + txtpass.Text;
                SmtpServer.Port = 25;
                SmtpServer.Credentials = new System.Net.NetworkCredential("realstate777@gmail.com", "realstate");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                //Label4.Text = ("Mail Sent Successfull");

            }
            catch (Exception emsg)
            {

            }
            Session["a_id"] = a_id;
            Response.Redirect("partner_add_agent_confirmation.aspx");

        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("../partner.aspx");
    }
}